package vip.xianz.jobmanager.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.validation.Errors;
import vip.xianz.jobmanager.exception.CheckException;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * 用于检查的工具类
 *
 * @author xian5
 * @create 2019 -05 -06  22:29
 */
public class CheckUtils {

    private static MessageSource source;
    public static void setSource(MessageSource source) {
        CheckUtils.source = source;
    }

    /**
     * 如果存在校验错误，直接返回校验失败，不明显校验结果
     * @param errors
     */
    public static void notErrors(Errors errors) {
        if (errors.hasErrors()) {
            fail("validate.error");
        }
    }

    public static void checkNull(Object obj, String msg, Object... args) {
        if (obj == null) {
            fail(msg, args);
        }
    }

    private static void fail(String msg, Object... args) {
        throw new CheckException(source.getMessage(msg, args, Locale.CANADA));
    }
}
